/**
 * Copyright (c) 2016 NumberFour AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   NumberFour AG - Initial API and implementation
 */
package org.eclipse.n4js.transpiler.es.tests;

import org.eclipse.n4js.N4JSInjectorProvider;
import org.eclipse.n4js.generator.GeneratorOption;
import org.eclipse.n4js.n4JS.Script;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests activation and deactivation of optional transformations in the transpiler via {@link GeneratorOption}s.
 *
 * NOTE: purpose of these tests is not to test the actual behavior of the optional transformations, just to see if
 * turning them on and off works.
 */
@RunWith(XtextRunner.class)
@InjectWith(N4JSInjectorProvider.class)
public class GeneratorOptionTest extends AbstractTranspilerTest {

	String snippet = """
				let b = 'b';
				`a${b}c`;

				async function foo(p1, p2, p3) {
					await bar();
				}
				async function bar() {}

				let arrow1 = ()=>{};
				let arrow2 = ()=>42;

				let x,y;
				({x,y}={x:1,y:2});
			""";

	@Test
	public void testTurnOnOptionalTransformations() throws Exception {
		Script script = testHelper.parseAndValidateSuccessfully(snippet);
		assertCompileResult(script, GeneratorOption.MAX_TRANSPILE_OPTIONS, """
				// Generated by N4JS transpiler; for copyright see original N4JS source file.

				import 'n4js-runtime'

				let b = 'b';
				("a" + b + "c");
				async function foo(p1, p2, p3) {
					await bar();
				}
				async function bar() {}
				let arrow1 = (function() {}).bind(this);
				let arrow2 = (function() {
					return 42;
				}).bind(this);
				let x, y;
				((function($destructParam0) {
					var $destruct0;
					$destruct0 = ($destructParam0);
					x = $destruct0['x'];
					y = $destruct0['y'];
					return $destruct0;
				})({
					x: 1,
					y: 2
				}));
				""");
	}

	@Test
	public void testTurnOffOptionalTransformations() throws Exception {
		Script script = testHelper.parseAndValidateSuccessfully(snippet);
		assertCompileResult(script, new GeneratorOption[0], """
				// Generated by N4JS transpiler; for copyright see original N4JS source file.

				import 'n4js-runtime'

				let b = 'b';
				`a${b}c`;
				async function foo(p1, p2, p3) {
					await bar();
				}
				async function bar() {}
				let arrow1 = ()=>{};
				let arrow2 = ()=>42;
				let x, y;
				({
					x,
					y
				} = {
					x: 1,
					y: 2
				});
				""");
	}
}
